import Vue from "vue";
import VueRouter from "vue-router";
import { Message } from 'element-ui';

Vue.use(VueRouter);

//解决重复点击导航栏路由报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

const login  = () => import('../page/login/Login')
const shopList  = () => import('../page/userWeb/shop/ShopList')
const goods  = () => import('../page/userWeb/goods/Goods')
const orderDetail  = () => import('../page/userWeb/orderDetail/OrderDetail')
const orderList  = () => import('../page/userWeb/orderList/OrderList')

const mgrPage  = () => import('../page/manager/tagPage/TagPage')
    const shopMgr = () => import('../page/manager/shopMgr/ShopMgr')
    const goodsMgr = () => import('../page/manager/goodsMgr/GoodsMgr')
    const orderMgr = () => import('../page/manager/orderMgr/OrderMgr')


const routes = [
    {
        path: '/',
        name: '',
        component: login,
        meta: {title: '登陆'}
    },
    {
        path: '/login',
        name: 'login',
        component: login,
        meta: {title: '登陆'}
    },
    {
        path: '/shopList',
        name: 'shopList',
        component: shopList,
        meta: {title: '商户列表'}
    },
    {
        path: '/goods',
        name: 'goods',
        component: goods,
        meta: {title: '商品列表'}
    },
    {
        path: '/orderDetail',
        name: 'orderDetail',
        component: orderDetail,
        meta: {title: '订单详情'}
    },
    {
        path: '/orderList',
        name: 'orderList',
        component: orderList,
        meta: {title: '订单列表'}
    },
    {
        path: '/mgrPage',
        name: 'mgrPage',
        component: mgrPage,
        children: [
            {
                path: '/mgrPage/shopMgr',
                name: 'shopMgr',
                component: shopMgr,
                meta: {title: '门店管理'}
            },
            {
                path: '/mgrPage/goodsMgr',
                name: 'goodsMgr',
                component: goodsMgr,
                meta: {title: '商品管理'}
            },
            {
                path: '/mgrPage/orderMgr',
                name: 'orderMgr',
                component: orderMgr,
                meta: {title: '订单管理'}
            },
        ]
    },
]



const router = new VueRouter({
    routes,
    mode: "history",
});




//前置钩子
router.beforeEach(async (to, from, next) => {
    if (to.path.indexOf("/login") != -1){
        next()
    }else {
        if (to.path.indexOf("/mgrPage") != -1){
            let userJson = localStorage.getItem('bus-user')
            if (!userJson){
                Message.info("请您重新登陆")
                router.push("/login")
            }
            let user = JSON.parse(userJson);
            if (user.type && user.type != 2){
                Message.info("当前账号无访问权限")
            } else {
                next()
            }
        }else {
            let userJson = localStorage.getItem('bus-user')
            if (!userJson){
                Message.info("请您重新登陆")
                router.push("/login")
            }
            let user = JSON.parse(userJson);
            if (user.type && user.type != 1){
                Message.info("当前登陆账号不适合访问前台")
            } else {
                next()
            }
        }
    }


})




export default router
